﻿using lBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace WebApplication191.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ShanController : ControllerBase
    {
        private readonly lUserBLL db;
        private readonly ILogger<ShanController> logger;

        public ShanController(lUserBLL db , ILogger<ShanController> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        /// <summary>
        /// 显示+查询+分页
        /// </summary>
        /// <param name="names">模糊查询</param>
        /// <param name="pageindex">总页数</param>
        /// <param name="pagesize">总条数</param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("GetAllAsyncList")]
        public dynamic GetAllAsyncList(string? names, int pageindex, int pagesize)
        {
            try
            {
                return db.GetAllAsyncList(names, pageindex, pagesize);
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("LjDele")]
        public Task<int> LjDele(int id)
        {
            try
            {
                return db.LjDele(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="idstring"></param>
        /// <returns>返回受影响函数</returns>
        [HttpGet("Plzt")]
        public Task<int> Plzt(string idstring)
        {
            try
            {
                return db.Plzt(idstring);
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="shanginfor"></param>
        /// <returns>返回受影响函数</returns>
        [HttpPost("AddAsyncss")]
        public Task<int> AddAsyncss(Shanginfor shanginfor)
        {
            try
            {
                logger.LogInformation("商品管理添加成功");
                return db.AddAsyncss(shanginfor);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet("Dao")]
        public IActionResult Dao()
        {
            var members = db.Dao();
            XSSFWorkbook wb = new XSSFWorkbook();
            var sheet = wb.CreateSheet("商品管理导出");
            var r = sheet.CreateRow(0);
            r.CreateCell(0).SetCellValue("项目名称");
            r.CreateCell(1).SetCellValue("项目编号");
            r.CreateCell(2).SetCellValue("服务时长");
            r.CreateCell(3).SetCellValue("服务人数");
            r.CreateCell(4).SetCellValue("佣金");
            r.CreateCell(5).SetCellValue("项目说明");
            r.CreateCell(6).SetCellValue("状态");
            int num = 1;
            foreach (var member in members)
            {
                var row = sheet.CreateRow(num);
                row.CreateCell(0).SetCellValue(member.ShanName);
                row.CreateCell(1).SetCellValue(member.ShanBian);
                row.CreateCell(2).SetCellValue(member.ShanShi);
                row.CreateCell(3).SetCellValue(member.ShanRen);
                row.CreateCell(4).SetCellValue(member.ShanYong);
                row.CreateCell(5).SetCellValue(member.ShanShuo);
                row.CreateCell(6).SetCellValue(member.ShanStart);
                num++;
            }
            byte[] bytes = null;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                bytes = ms.ToArray();
            }
            string mime = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mime, "商品管理导出.xlsx");
        }
    }
}
